package com.whu.helpluojia.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.whu.helpluojia.common.BaseContext;
import com.whu.helpluojia.common.R;
import com.whu.helpluojia.common.UserException;
import com.whu.helpluojia.dao.UserDao;
import com.whu.helpluojia.domain.User;
import com.whu.helpluojia.domain.dto.UserDto;
import com.whu.helpluojia.service.UserService;
import com.whu.helpluojia.util.EmailUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    JavaMailSender javaMailSender;

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @PostMapping("/login")
    public void login(@RequestBody User user, HttpSession session){

        User userLogin = userService.getUserLogin(user);

        if(userLogin != null){
            session.setAttribute("user",userLogin.getId());
        }

    }

    @PostMapping("/code")
    public void code(@RequestBody User user){

        String email=user.getEmail();

        if(email!=null){
            if(email.substring(email.length()-11).equals("@whu.edu.cn")){

                Integer codeInt = EmailUtil.generateCode();
                String code=codeInt.toString();

                sendEmail(email,code);

                redisTemplate.opsForValue().set(email,code,10, TimeUnit.MINUTES);

            }
        }


    }

    @PostMapping("/register")
    public void register(@RequestBody UserDto userdto){

        userService.register(userdto);

    }

    @GetMapping("/userId")
    public  Long getUserId(){
        return BaseContext.getCurrentId();
    }

    @GetMapping("/userInfo/{id}")
    public User getUserInfo(@PathVariable Long id) {
        User user = userService.selectUser(id);
        if(user == null){
            return null;
        }
        return user;
    }

    @PostMapping("/updateInfo")
    public void updateInfo(@RequestBody User user){
        userService.updateById(user);
    }

    @GetMapping("/mineInfo")
    public User getUserInfoMine(){
        System.out.println("user:");
        Long id = BaseContext.getCurrentId();
        User user = userService.selectUser(id);
        return user;
    }

    public void sendEmail(String destination,String code){
        log.info(destination+" "+code);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("互助珞珈验证码");
        message.setFrom("1468735019@qq.com");
        message.setTo(destination);
        message.setSentDate(new Date());
        message.setText("您的验证码是："+code+"，有效期10分钟。");
        javaMailSender.send(message);
    }
}
